﻿using Microsoft.AspNet.Scaffolding;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MVCScaffolder
{

    /// <summary>
    /// Searches the Context Active project for JQuery Versions.
    /// </summary>
    public class JQueryFigureOuter 
    {
        public Boolean FoundJQueryVersion { get; set; }
        public String JQueryVersion { get; set; }

        public Boolean FoundJQueryUIVersion { get; set; }
        public String JQueryUIVersion { get; set; }

        public JQueryFigureOuter(CodeGenerationContext Context)
        {
            FoundJQueryVersion = false;
            FoundJQueryUIVersion = false;

            // Search the Active Project Items
            foreach (EnvDTE.ProjectItem ProjectItem in Context.ActiveProject.ProjectItems)
            {
                // If there is a Scripts folder
                if (ProjectItem.Name == "Scripts")
                {
                    // Go through the Scripts Folder
                    foreach (EnvDTE.ProjectItem ScriptsItem in ProjectItem.ProjectItems)
                    {
                        // Haven't found JQuery Version
                        if (FoundJQueryVersion == false)
                        {
                            // Find a file that looks promising
                            if (ScriptsItem.Name.Contains("jquery-"))
                            {
                                // Strip off the junk
                                String WorkingVersionName = ScriptsItem
                                    .Name
                                    .Replace("jquery-", "")
                                    .Replace("ui-", "")
                                    .Replace(".min", "")
                                    .Replace(".map", "")
                                    .Replace(".intellisense","")
                                    .Replace(".js", "");
                                    

                                // Hopefully this is a version number.
                                if (WorkingVersionName.Length > 0)
                                {
                                    FoundJQueryVersion = true;
                                    JQueryVersion = WorkingVersionName;
                                }
                            }
                        }

                        // Have we found the JQueryUI Version?
                        if (FoundJQueryUIVersion == false)
                        {
                            // See if an item looks promising
                            if (ScriptsItem.Name.Contains("jquery-ui-"))
                            {
                                // strip off the junk.
                                String WorkingVersionName = ScriptsItem
                                    .Name
                                    .Replace("jquery-ui-", "")
                                    .Replace(".min", "")
                                    .Replace(".map", "")
                                    .Replace(".intellisense", "")
                                    .Replace(".js", "");

                                // Anything left?
                                if (WorkingVersionName.Length > 0)
                                {
                                    // lets hope this works.
                                    FoundJQueryUIVersion = true;
                                    JQueryUIVersion = WorkingVersionName;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
